﻿namespace AddressBook.Business.Contact
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	[Serializable]
	public class ContactEmailAddresses : BusinessListBase<ContactEmailAddresses, ContactEmailAddress>
  {
    #region "Business Methods"

    public ContactEmailAddress GetItem(Int32 emailAddressId)
    {
      foreach (ContactEmailAddress address in this)
      {
        if (address.EmailAddressId == emailAddressId)
        {
          return address;
        }
      }

      return null;
    }

    public void Add(Int32 emailAddressTypeId)
    {
      if (!(Contains(emailAddressTypeId)))
      {
        ContactEmailAddress resource = ContactEmailAddress.NewEmailAddress(emailAddressTypeId);
        this.Add(resource);
      }
      else
      {
        throw new InvalidOperationException("Contact already has an email address of that type");
      }
    }

    public void Add(String emailAddress, Int32 emailAddressTypeId)
    {
      if (!(Contains(emailAddressTypeId)))
      {
        ContactEmailAddress resource = ContactEmailAddress.NewEmailAddress(emailAddress, emailAddressTypeId);
        this.Add(resource);
      }
      else
      {
        throw new InvalidOperationException("Contact already has an email address of that type");
      }
    }

    public void Remove(Int32 emailAddressId)
    {
      foreach (ContactEmailAddress address in this)
      {
        if (address.EmailAddressId == emailAddressId)
        {
          Remove(address);
          break;
        }
      }
    }

    public bool Contains(Int32 emailAddressTypeId)
    {
      foreach (ContactEmailAddress address in this)
      {
        if (address.EmailAddressTypeId == emailAddressTypeId)
        {
          return true;
        }
      }
      return false;
    }

    public bool ContainsDeleted(Int32 emailAddressTypeId)
    {
      foreach (ContactEmailAddress address in DeletedList)
      {
        if (address.EmailAddressTypeId == emailAddressTypeId)
        {
          return true;
        }
      }
      return false;
    }

    #endregion

    #region "Factory Methods"

    internal static ContactEmailAddresses NewContactEmailAddresses()
    {
      return DataPortal.CreateChild<ContactEmailAddresses>();
    }

		internal static ContactEmailAddresses GetContactEmailAddresses(AddressBook.Business.Model.ContactEmailAddress[] data)
    {
      return DataPortal.FetchChild<ContactEmailAddresses>(data);
    }

		private ContactEmailAddresses()
    { /* require use of factory methods */ }

    #endregion

    #region "Data Access"

    private void Child_Fetch(AddressBook.Business.Model.ContactEmailAddress[] data)
    {
      if (data == null) return;

      this.RaiseListChangedEvents = false;

      foreach (var value in data)
      {
        this.Add(ContactEmailAddress.GetEmailAddress(value));
      }

      this.RaiseListChangedEvents = true;
    }

    #endregion
  }
}
